using System;
using System.ComponentModel;
using System.ComponentModel.Design;

using System.Drawing;
using System.Drawing.Drawing2D;

namespace Camalon.Drawing
{

	/// <summary>
	/// Summary description for ShapeBrush.
	/// </summary>
	[ToolboxItem( true )]
	[ToolboxBitmap( typeof( ShapePen ), "Components.Outline.bmp")]
    [Designer("Camalon.Drawing.Design.OutlineDesigner, Camalon.Drawing.Design", typeof(IDesigner))]
    public class Outline : ShapePenBase
	{
		float				_flatness;
		bool				_transform;

		#region Constructor

//		public Outline(System.ComponentModel.IContainer container):this()
//		{
//			container.Add( this);
//		}

		public Outline():base(10f)
		{
			//DEFAULTS
			//-------------------------
			_flatness		= 1.0f;
			_transform		= true;

			//--------------------------
		}


		public Outline(float width):base(width)
		{
		}

		public Outline(float width, float flatness):base(width)
		{
			_flatness = flatness; 
		}



		#endregion Constructor

		[Browsable( false)]
		public Pen Pen
		{
			get
			{
				Pen pen = new Pen( Color.Empty );

				
				pen.Alignment		= PenAlignment.Center;

				int[] compoundArray = base.Compounds;
				if( compoundArray.Length > 1 ) 
				{
					float[] compounds = new float[compoundArray.Length];
					for (int i = 0; i < compounds .Length; i++) compounds[i] = ((float)compoundArray[i])/100f;
					pen.CompoundArray	= compounds;
				}
				
				pen.DashCap			= base.DashCap;
				pen.DashOffset		= base.DashOffsetGDI;// base.DashOffset;

				if( base.DashStyle == DashStyle.Custom  ) 
				{	
					float[] dashValues = base.DashPatternGDI;// ValidateDashPattern( );
					if( dashValues != null) pen.DashPattern		= dashValues;
					else pen.DashStyle = DashStyle.Solid;
				}
				else pen.DashStyle		= base.DashStyle;

				pen.LineJoin		= base.LineJoin;
				pen.MiterLimit		= base.MiterLimit;
				pen.Width			= base.Width;

				base.MakeCap( pen);

				return pen;

			}
		}


		public static Outline GetOutline( ShapePen shapePen )
		{
			if( shapePen == null) return null;
			
			Outline outline = new Outline();

			outline.Compounds		= shapePen.Compounds;
			outline.DashCap			= shapePen.DashCap;
			outline.DashOffset		= shapePen.DashOffsetGDI;//shapePen.DashOffset;
			outline.DashPattern		= shapePen.DashPattern;
			outline.DashStyle		= shapePen.DashStyle;
			outline.EndCap			= shapePen.EndCap;
			outline.Flatness		= 1.0f;
			outline.LineJoin		= shapePen.LineJoin;
			outline.MiterLimit		= shapePen.MiterLimit;
			outline.StartCap		= shapePen.StartCap;
			outline.Width			= shapePen.Width;
			outline.Transform		= true;
			

			return outline;

		}


		public static Pen GetOutlinePen( ShapePen shapePen )
		{
			if( shapePen == null) return null;

			using(Outline outline = Outline.GetOutline(shapePen))
			{
				if( outline == null) return null;
				return outline.Pen;
			}
		}


		#region Properties

		const float MINF = 0.1f;//Min Flatnes
		[DefaultValue(1.0f)]
		[Description("A value that specifies the flatness for curves.")]
		[RefreshProperties(RefreshProperties.Repaint)]
		public float Flatness
		{
			get
			{	
				return _flatness;
			} 
			set
			{ 
				
				if( value < 0) value = MINF;//1
				if( value < MINF) value = MINF;
				if( value == _flatness ) return;
				_flatness = value;
				InvokeInternalChanged();
			}
		}


		[DefaultValue(true)]
		[Description("Specifies that the Shape objects shold be outlined after the transformation applied.")]
		[RefreshProperties(RefreshProperties.Repaint)]
		public bool Transform
		{
			get{ return _transform;} 
			set
			{ 
				if( value == _transform ) return;
				_transform = value;
				InvokeInternalChanged();
			}
		}



		#endregion Properties

	}
}
